home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / cenvid.zip / ALLFILES.BAT < prev    next >
DOS Batch File  |  1993-06-06  |  4KB  |  109 lines

  1. @echo off
  2. REM ************************************************************************
  3. REM *** AllFiles.bat - Perform the given command on all files that match ***
  4. REM ***                the input command spec, where the special command ***
  5. REM ***                fields $DIR, $ROOT, $EXT, and $FILE are replaced  ***
  6. REM ***                by the directory, root, extension, and full       ***
  7. REM ***                filename of the matching files found.             ***
  8. REM ************************************************************************/
  9.  
  10. REM **********************************************************************
  11. REM *** Only nine parameters can be passed using %1 %2 %3 etc, and so  ***
  12. REM *** build up one big environment variable to hold all the parms,   ***
  13. REM *** and then pass that big variable to the CEnvi code.             ***
  14. REM **********************************************************************
  15. SET ALLFILES_SOURCE=%0
  16. SET ALLFILES_ARGUMENTS=
  17.    :NEXT_ARG
  18.       IF a%1z == az GOTO NO_MORE_ARGS
  19.       SET ALLFILES_ARGUMENTS=%ALLFILES_ARGUMENTS% %1
  20.       SHIFT
  21.       GOTO NEXT_ARG
  22.    :NO_MORE_ARGS
  23. CEnvi %ALLFILES_SOURCE%.bat %ALLFILES_ARGUMENTS%
  24. GOTO CENVI_EXIT
  25.  
  26. main(argc,argv)
  27. {
  28.    if ( argc < 3  ||  !strcmp(argv[1],"/?")  ||  !stricmp(argv[1],"/help") ) {
  29.       Instructions();
  30.    } else {
  31.       FileSpec = argv[1];
  32.       // build output command, which is all the other input args
  33.       Command = argv[2];
  34.       for ( i = 3; i < argc; i++ ) {
  35.          strcat(strcat(Command," "),argv[i]);
  36.       }
  37.       // build list of matching files
  38.       FileList = Directory(FileSpec,FALSE,FATTR_RDONLY|FATTR_HIDDEN|FATTR_SYSTEM|FATTR_ARCHIVE)
  39.       if ( NULL != FileList ) {
  40.          for( i = 0; i <= GetArraySpan(FileList); i++ ) {
  41.             PerformCommandOnFile(Command,FileList[i].name);
  42.          }
  43.       }
  44.    }
  45. }
  46.  
  47. SubstituteNamesInCommand(Command,Find,Replace)
  48. {
  49.    FindLen = strlen(Find);
  50.    ReplaceLen = strlen(Replace);
  51.    for ( c = Command; NULL != (c = strchr(c,'$')); c++ ) {
  52.       if ( !strnicmp(c+1,Find,FindLen) ) {
  53.          // replace the Find string with the Replace string
  54.          strcpy(c+ReplaceLen,c+1+FindLen);
  55.          memcpy(c,Replace,ReplaceLen);
  56.          c += ReplaceLen - 1;
  57.       }
  58.    }
  59. }
  60.  
  61. PerformCommandOnFile(VirginCommand,File)
  62. {
  63.    NameParts = SplitFileName(File);
  64.    // substitute all the $DIR, $ROOT, $EXT, and $FILE commands for that part in NameParts
  65.    strcpy(NewCommand,VirginCommand);
  66.    SubstituteNamesInCommand(NewCommand,"DIR",NameParts.dir);
  67.    SubstituteNamesInCommand(NewCommand,"ROOT",NameParts.name);
  68.    SubstituteNamesInCommand(NewCommand,"EXT",NameParts.ext);
  69.    SubstituteNamesInCommand(NewCommand,"FILE",File);
  70.    // set the environment variables to the new values
  71.    $DIR = NameParts.dir
  72.    $ROOT = NameParts.name
  73.    $EXT = NameParts.ext
  74.    $FILE = File
  75.    // print the command, and send it to the system
  76.    printf("%s\n",NewCommand);
  77.    system(NewCommand);
  78. }
  79.  
  80. Instructions()
  81. {
  82.    printf("\n")
  83.    printf("AllFiles.bat - Execute a command on all files meeting the input criteria.  The\n")
  84.    printf("               command may include these strings:\n")
  85.    printf("                $DIR    - directory portion of matching file\n")
  86.    printf("                $ROOT   - root name of the matching file\n")
  87.    printf("                $EXT    - extension name of the matching file\n")
  88.    printf("                $FILE   - full name of the matching file\n")
  89.    printf("               These names, $DIR, $ROOT, $EXT, and $FILE will also be set as\n")
  90.    printf("               environment variables when the command is executed.\n")
  91.    printf("\n")
  92.    printf("SYNTAX: AllFiles FileSpec [Command]\n")
  93.    printf("Where:  FileSpec  - any file specification ith wildcards included\n")
  94.    printf("        Command   - any command line specification\n")
  95.    printf("\n")
  96.    printf("EXAMPLE: To copy all *.TXT files to *.bak files in the C:\\Temp directory. but\n")
  97.    printf("         only if the C:\\Temp\\*.bak file does not already exist:\n")
  98.    printf("     ALLFILES *.TXT IF NOT EXIST C:\\Temp\\$ROOT.bak copy $FILE C:\\Temp\\$ROOT.bak\n")
  99.    printf("\n")
  100. }
  101.  
  102. :CENVI_EXIT
  103.    SET ALLFILES_SOURCE=
  104.    SET ALLFILES_ARGUMENTS=
  105.    SET $DIR=
  106.    SET $ROOT=
  107.    SET $EXT=
  108.    SET $FILE=
  109.